using System.Collections;
using System.Collections.Generic;
using Main.Event;
using Main.Map;
using UnityEngine;
using ZSW.Framework;

namespace Main.CropPlant
{
    public class CropGenerator : ZSWF_InitMonoBehaviour
    {
        public int SeedItemID;

        public int growDays;

        public override void Init()
        {
            base.Init();

            MainEventSystem.GenerateCropEvent += GenerateCrop;
        }

        private void OnDisable()
        {
            MainEventSystem.GenerateCropEvent -= GenerateCrop;
        }

        private void GenerateCrop()
        {
            Vector3Int cropGridPos = FindObjectOfType<Grid>().WorldToCell(transform.position);

            if (SeedItemID != 0)
            {
                if (!ZSWF_IOCContainer.Get<GridMapManager>().TryGetCurrentSceneTileDetails(cropGridPos, out var tileDetails))
                {
                    tileDetails = new Details.TileDetails();
                    tileDetails.girdX = cropGridPos.x;
                    tileDetails.girdY = cropGridPos.y;
                }

                tileDetails.daysSinceWatered = -1;
                tileDetails.seedItemID = this.SeedItemID;
                tileDetails.growDays = this.growDays;

                ZSWF_IOCContainer.Get<GridMapManager>().UpdateTileDetails(tileDetails);
            }
        }
    }
}